#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
bool Begin;
int n,q,L[N],R[N],now[N];
ll f[N];
bool End;
inline void Min(ll &x,ll y) {x=x>y?y:x;}
int main()
{
    freopen("block.in","r",stdin);
    freopen("block.out","w",stdout);
    cerr<<(&Begin-&End)/1024/1024<<"MB\n";
    memset(f,0x3f,sizeof(f));
    scanf("%d%d",&n,&q);
    for(int i=1;i<=q;i++) scanf("%d%d",L+i,R+i);
    f[0]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=i-1;~j;j--)
        {
            int sum=0;
            for(int k=1;k<=q;k++)
                if(L[k]<=j&&i<=R[k]) ++sum;
                else if(j<=L[k]&&R[k]<=i) sum+=(i+L[k]-R[k]-j);
                else if(j<=L[k]&&L[k]<=i) sum+=min(L[k]-j,i-L[k]);
                else if(j<=R[k]&&R[k]<=i) sum+=min(R[k]-j,i-R[k]);
            Min(f[i],f[j]+sum);
        }
    }
    printf("%lld\n",f[n]);
    return 0;
}